package org.skydiveapplibrary.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.skydiveapplibrary.data.Database;
import org.skydiveapplibrary.repository.LogEntryRepository;
import org.skydiveapplibrary.ui.FormatUtility;
import org.skydiveapplibrary.R;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.TextView;

public class LogEntryListCursorAdapter extends CursorAdapter
{
	private List<Integer> selectedIds = new ArrayList<Integer>();
	public Boolean multiSelect = false;
	
	public LogEntryListCursorAdapter(Context context, Cursor cursor, int flags)
	{
		super(context, cursor, flags);
	}
	
	public void clearSelection()
	{
		selectedIds.clear();
	}
	
	public boolean toggleSelection(int id)
	{
		if (!multiSelect)
			clearSelection();
		
		// box it
		Integer boxedId = id;
		if (selectedIds.contains(boxedId))
		{
			selectedIds.remove(boxedId);
			return false;
		}
		else
		{		
			selectedIds.add(boxedId);
			return true;
		}
	}
	
	public ArrayList<Integer> getSelection()
	{
		return (ArrayList<Integer>)selectedIds;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{
		// get data
		Integer id = Database.getInteger(cursor, Database.LOG_ENTRY_ID);
		int jumpNumber = Database.getInteger(cursor, Database.LOG_ENTRY_JUMP_NUMBER);
		String skydiveTypeName = Database.getString(cursor, LogEntryRepository.SKYDIVE_TYPE_NAME);
		String locationName = Database.getString(cursor, LogEntryRepository.LOCATION_NAME);
		String aircraftName = Database.getString(cursor, LogEntryRepository.AIRCRAFT_NAME);
		Date date = Database.getDate(cursor, Database.LOG_ENTRY_DATE);
		String notes = Database.getString(cursor, Database.LOG_ENTRY_NOTES);
		Integer signatureId = Database.getInteger(cursor, LogEntryRepository.SIGNATURE_ID);
		
		// set jump number
		setText(
				view,
				R.id.LogEntryListItemJumpNumberField,
				FormatUtility.toNumericString(jumpNumber));
		
		// set location
		setText(
				view,
				R.id.LogEntryListItemLocationField,
				locationName);
		
		// set date
		setText(
				view,
				R.id.LogEntryListItemDateTimeField,
				FormatUtility.toDateString(context, date));
		
		// set skydive type
		setText(
				view,
				R.id.LogEntryListItemSkydiveTypeField,
				skydiveTypeName,
				true);
		
		// set aircraft
		setText(
				view,
				R.id.LogEntryListItemAircraftField,
				aircraftName,
				true);
		
		// set notes
		setHint(
				view,
				R.id.LogEntryListItemNotesField,
				notes);
		
		// set signed
		ImageView signedField = (ImageView)view.findViewById(R.id.LogEntryListSignedField);
		
		signedField.setVisibility(signatureId == null ? View.GONE : View.VISIBLE);
		
		ImageView selectedField = (ImageView)view.findViewById(R.id.LogEntryListSelectedFieldField);

		// set selected state
		if (multiSelect)
		{
			if (selectedIds.contains(id))
			{
				selectedField.setImageResource(R.drawable.btn_check_on);
			}
			else
			{
				selectedField.setImageResource(R.drawable.btn_check_off);
			}
		}
		else
			selectedField.setImageDrawable(null);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent)
	{
		// inflate view
		return View.inflate(
				context,
				R.layout.log_entry_view,
				null);
	}
	
	private void setText(View parentView, int id, String text)
	{
		setText(parentView, id, text, false);
	}
	
	private void setText(View parentView, int id, String text, boolean hideIfEmpty)
	{
		TextView tv = (TextView)parentView.findViewById(id);
		tv.setText(text);
		tv.setVisibility(hideIfEmpty && FormatUtility.isEmptyOrNull(text) ? View.GONE : View.VISIBLE);
	}
	
	private void setHint(View parentView, int id, String text)
	{
		TextView tv = (TextView)parentView.findViewById(id);
		tv.setHint(text);
		tv.setVisibility(FormatUtility.isEmptyOrNull(text) ? View.GONE : View.VISIBLE);
	}
}
